/**
 * @author Ahammer
 * @date 2009 05
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollBar;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import main.IqmTools;
import main.Tank;

public class IqmTurboRegGUI  extends IqmOperatorJFrame
							    implements IqmOperatorGUIBuilder, ActionListener, AdjustmentListener {

	private  ParameterBlockJAI pb = null;  //  @jve:decl-index=0:
		
	private  JRadioButtonMenuItem buttFast 			= null;
	private  JRadioButtonMenuItem buttAccurate		= null;
	private  JRadioButtonMenuItem buttRegModeHidden	= null;
	private  JLabel 			  jLabelRegModeText	= null;
	private  JToolBar 			  jToolBarRegMode  	= null;
	private  ButtonGroup 		  buttGroupRegMode 	= null;
		
	private  JRadioButtonMenuItem buttMethodTrans 	= null;
	private  JRadioButtonMenuItem buttMethodRigBod	= null;
	private  JRadioButtonMenuItem buttMethodScalRot	= null;
	private  JRadioButtonMenuItem buttMethodAff		= null;
	private  JRadioButtonMenuItem buttMethodBiLin	= null;
	private  JRadioButtonMenuItem buttMethodHidden	= null;
	private  JLabel 			  jLabelMethodText	= null;
	private  JToolBar 			  jToolBarMethod  	= null;
	private  ButtonGroup 		  buttGroupMethod 	= null;
		
	/**
	 * constructor
	 */
	public IqmTurboRegGUI() {

	}


	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pb
	 *
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pb = arg;
		IqmTools.setCurrParameterBlockJAI(pb);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){
		if (buttFast.isSelected())     	pb.setParameter("RegMode", 0);
		if (buttAccurate.isSelected())  pb.setParameter("RegMode", 1);
	
		if (buttMethodTrans.isSelected())   pb.setParameter("Method", 0);
		if (buttMethodRigBod.isSelected())  pb.setParameter("Method", 1);
		if (buttMethodScalRot.isSelected()) pb.setParameter("Method", 2);
		if (buttMethodAff.isSelected())     pb.setParameter("Method", 3);
		if (buttMethodBiLin.isSelected())   pb.setParameter("Method", 4);
				
		IqmTools.setCurrParameterBlockJAI(pb);	
	}
	
	/**
	 * This method sets the current parameter values
	 */
	@Override
	public void setParameterValues(){
		int idx = IqmTools.getCurrManagerImgIndex();
	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	       if (pi == null){
	    	   System.out.println("IqmTurboRegGUI: Current image not defined");
	    	   return;
	       }
        //String type = IqmTools.getCurrImgTyp(pi);

		buttRegModeHidden.setSelected(true);
		if (pb.getIntParameter("RegMode") == 0 ) buttFast.setSelected(true);		
		if (pb.getIntParameter("RegMode") == 1 ) buttAccurate.setSelected(true);		
					
		buttMethodHidden.setSelected(true);		
		if (pb.getIntParameter("Method") == 0) buttMethodTrans.setSelected(true);		
		if (pb.getIntParameter("Method") == 1) buttMethodRigBod.setSelected(true);		
		if (pb.getIntParameter("Method") == 2) buttMethodScalRot.setSelected(true);
		if (pb.getIntParameter("Method") == 3) buttMethodAff.setSelected(true);
		if (pb.getIntParameter("Method") == 4) buttMethodBiLin.setSelected(true);
		
	}
		

	/**
	 * This method creates and shows the GUI
	 */
	@Override
	public void createIqmOperatorGUI() {
		//opGUIFrame = new ImgProcFuncJFrame();  //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame = new IqmTurboRegGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//this.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("TurboReg");
		PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());
		//int numBands = pi.getNumBands();
		//Dimension dim = this.getSize();
		//this.setSize(dim.width, dim.height + 200);
		//this.setPreferredSize(new Dimension(800, 600));
		this.getJContentPane().add(getJLabelRegModeText(),  getGridBagConstraintsRegModeText());
		this.getJContentPane().add(getJToolBarRegMode(),    getGridBagConstraintsButtonGroupRegMode());
		this.getJContentPane().add(getJLabelMethodText(),  getGridBagConstraintsMethodText());
		this.getJContentPane().add(getJToolBarMethod(),    getGridBagConstraintsButtonGroupMethod());

		this.pack();
		//this.repaint();
		if (pb == null) pb = new ParameterBlockJAI("IqmTurboReg");  //nur wenn das erste mal aufgerufen
		//pb = new ParameterBlockJAI("iqmTurboReg"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pb);
		setParameterValues();
	}

	//------------------------------------------------------------------------------
	private  GridBagConstraints getGridBagConstraintsRegModeText(){
		GridBagConstraints gridBagConstraintsRegModeText = new GridBagConstraints();
		gridBagConstraintsRegModeText.gridx = 1;
		gridBagConstraintsRegModeText.gridy = 4;
		//gridBagConstraintsRegModeText.gridwidth = 3;
		gridBagConstraintsRegModeText.insets = new Insets(15,15,0,0);  //top, left bottom right
		gridBagConstraintsRegModeText.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsRegModeText;
	}
	private  GridBagConstraints getGridBagConstraintsButtonGroupRegMode(){
		GridBagConstraints gridBagConstraintsButtonGroupRegMode = new GridBagConstraints();
		gridBagConstraintsButtonGroupRegMode.gridx = 2;
		gridBagConstraintsButtonGroupRegMode.gridy = 4;
		gridBagConstraintsButtonGroupRegMode.gridwidth = 2;
		gridBagConstraintsButtonGroupRegMode.insets = new Insets(10,0,0,15);  //top, left bottom right
		gridBagConstraintsButtonGroupRegMode.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonGroupRegMode;
	}


	private  GridBagConstraints getGridBagConstraintsMethodText(){
		GridBagConstraints gridBagConstraintsMethodText = new GridBagConstraints();
		gridBagConstraintsMethodText.gridx = 1;
		gridBagConstraintsMethodText.gridy = 6;
		//gridBagConstraintsMethodText.gridwidth = 3;
		gridBagConstraintsMethodText.insets = new Insets(10,15,0,0);  //top, left bottom right
		gridBagConstraintsMethodText.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsMethodText;
	}
	private  GridBagConstraints getGridBagConstraintsButtonGroupMethod(){
		GridBagConstraints gridBagConstraintsButtonGroupMethod = new GridBagConstraints();
		gridBagConstraintsButtonGroupMethod.gridx = 2;
		gridBagConstraintsButtonGroupMethod.gridy = 6;
		gridBagConstraintsButtonGroupMethod.gridwidth = 2;
		gridBagConstraintsButtonGroupMethod.insets = new Insets(10,0,20,15);  //top, left bottom right
		gridBagConstraintsButtonGroupMethod.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonGroupMethod;
	}



	//------------------------------------------------------------------------------------------------------------------
	/**
	 * This method updates the GUI if needed
	 * This method overrides IqmOperatorJFrame
	 */
	@Override
	public void updateIqmOperatorGUI(){
		
	}
	/**
	 * This method initializes jLabelRegModeText	
	 * @return javax.swing.JLabel	
	 */
	private  JLabel getJLabelRegModeText() {
		if (jLabelRegModeText == null) {
			jLabelRegModeText = new JLabel();
			jLabelRegModeText.setText("Registration Mode:");
			jLabelRegModeText.setEnabled(false);
		}
		return jLabelRegModeText	;
	}
	/**
	 * This method initializes the Option: Fast 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtFast() {
		//if (buttFast == null) {
			buttFast = new JRadioButtonMenuItem();
			buttFast.setText("Fast");
			buttFast.setToolTipText("fast registration");
			buttFast.addActionListener(this);
			buttFast.setActionCommand("parameter");
			buttFast.setEnabled(false);
		//}
		return buttFast;
	}
	/**
	 * This method initializes the Option: Accurate
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtAccurate() {
		//if (buttAccurate == null) {
			buttAccurate = new JRadioButtonMenuItem();
			buttAccurate.setText("Accurate");
			buttAccurate.setToolTipText("accurate registration");
			buttAccurate.addActionListener(this);
			buttAccurate.setActionCommand("parameter");
			buttAccurate.setEnabled(false);
		//}
		return buttAccurate;
	}

	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtRegModeHidden() {
		//if (buttRegModeHidden == null) {
			buttRegModeHidden  = new JRadioButtonMenuItem();
			buttRegModeHidden .setText("Hidden");
			buttRegModeHidden.setVisible(false);

		//}
		return buttRegModeHidden;
	}
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarRegMode() {
//		if (jToolBarRegMode == null) {
			jToolBarRegMode = new JToolBar("Options");
			jToolBarRegMode.setOrientation(1); //
			jToolBarRegMode.add(getJRadioButtonMenuButtFast());
			jToolBarRegMode.add(getJRadioButtonMenuButtAccurate());			
			jToolBarRegMode.add(getJRadioButtonMenuButtRegModeHidden());
			//jToolBarRGB.addSeparator();
			this.setButtonGroupRegMode();	//Gruppieren der JRadioButtons
//		}
		return jToolBarRegMode;
	}
	private  void setButtonGroupRegMode() {
		//if (ButtonGroup buttGroupRegMode == null) {
			buttGroupRegMode = new ButtonGroup();
			buttGroupRegMode.add(buttFast);
			buttGroupRegMode.add(buttAccurate);
			buttGroupRegMode.add(buttRegModeHidden);		
	}
	


	//-----------------------------------------------------------------------------------------
	/**
	 * This method initializes jLabelMethodText	
	 * @return javax.swing.JLabel	
	 */
	private  JLabel getJLabelMethodText() {
		if (jLabelMethodText == null) {
			jLabelMethodText = new JLabel();
			jLabelMethodText.setText("Method:");
		}
		return jLabelMethodText	;
	}
	/**
	 * This method initializes the Option: MethodTrans 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtMethodTrans() {
		//if (buttMethodTrans == null) {
			buttMethodTrans = new JRadioButtonMenuItem();
			buttMethodTrans.setText("Translation");
			buttMethodTrans.setToolTipText("translation registration");
			buttMethodTrans.addActionListener(this);
			buttMethodTrans.setActionCommand("parameter");
		//}
		return buttMethodTrans;
	}
	/**
	 * This method initializes the Option: MethodRigBod
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtMethodRigBod() {
		//if (buttMethodRigBod == null) {
			buttMethodRigBod = new JRadioButtonMenuItem();
			buttMethodRigBod.setText("Rigid Body");
			buttMethodRigBod.setToolTipText("rigid body registration");
			buttMethodRigBod.addActionListener(this);
			buttMethodRigBod.setActionCommand("parameter");
		//}
		return buttMethodRigBod;
	}
	/**
	 * This method initializes the Option: MethodScalRot
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtMethodScalRot() {
		//if (buttMethodScalRot == null) {
			buttMethodScalRot = new JRadioButtonMenuItem();
			buttMethodScalRot.setText("Scaled Rotation");
			buttMethodScalRot.setToolTipText("scaled rotation registraion");
			buttMethodScalRot.addActionListener(this);
			buttMethodScalRot.setActionCommand("parameter");
		//}
		return buttMethodScalRot;
	}
	/**
	 * This method initializes the Option: MethodAff
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtMethodAff() {
		//if (buttMethodAff == null) {
			buttMethodAff = new JRadioButtonMenuItem();
			buttMethodAff.setText("Affine");
			buttMethodAff.setToolTipText("affine registration");
			buttMethodAff.addActionListener(this);
			buttMethodAff.setActionCommand("parameter");
		//}
		return buttMethodAff;
	}
	/**
	 * This method initializes the Option: MethodBiLin
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtMethodBiLin() {
		//if (buttMethodBiLin == null) {
			buttMethodBiLin = new JRadioButtonMenuItem();
			buttMethodBiLin.setText("Bilinear");
			buttMethodBiLin.setToolTipText("bilinear registration");
			buttMethodBiLin.addActionListener(this);
			buttMethodBiLin.setActionCommand("parameter");
		//}
		return buttMethodBiLin;
	}
	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtMethodHidden() {
		//if (buttMethodHidden == null) {
			buttMethodHidden  = new JRadioButtonMenuItem();
			buttMethodHidden .setText("Hidden");
			buttMethodHidden.setVisible(false);

		//}
		return buttMethodHidden;
	}
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarMethod() {
//		if (jToolBarMethod == null) {
			jToolBarMethod = new JToolBar("Options");
			jToolBarMethod.setOrientation(1); //
			jToolBarMethod.add(getJRadioButtonMenuButtMethodTrans());
			jToolBarMethod.add(getJRadioButtonMenuButtMethodRigBod());			
			jToolBarMethod.add(getJRadioButtonMenuButtMethodScalRot());
			jToolBarMethod.add(getJRadioButtonMenuButtMethodAff());
			jToolBarMethod.add(getJRadioButtonMenuButtMethodBiLin());
			jToolBarMethod.add(getJRadioButtonMenuButtMethodHidden());

			//jToolBarRGB.addSeparator();
			this.setButtonGroupMethod();	//Gruppieren der JRadioButtons
//		}
		return jToolBarMethod;
	}
	private  void setButtonGroupMethod() {
		//if (ButtonGroup buttGroupMethod == null) {
			buttGroupMethod = new ButtonGroup();
			buttGroupMethod.add(buttMethodTrans);
			buttGroupMethod.add(buttMethodRigBod);
			buttGroupMethod.add(buttMethodScalRot);
			buttGroupMethod.add(buttMethodAff);
			buttGroupMethod.add(buttMethodBiLin);
			buttGroupMethod.add(buttMethodHidden);		
	}	
	//--------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmTurboRegGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pb = new ParameterBlockJAI("IqmTurboReg".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pb);
			this.setParameterBlock(pb);
			this.setParameterValues();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {
			this.setParameterBlock();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		}

		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}
	//------------------------------------------------------------------------------------------------
	@Override
	public void adjustmentValueChanged(AdjustmentEvent e) {
		Object obE = e.getSource();
		if (obE instanceof JScrollBar){		
			//ScrollBars--------------------		
		}
		this.setParameterBlock();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
	}

	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmTurboRegGUI gui = new IqmTurboRegGUI();
				gui.createIqmOperatorGUI();

			}
		});
	}



	
}//END
